package com.lyy.blog.web.admin;

import com.lyy.blog.po.blog;
import com.lyy.blog.po.user;
import com.lyy.blog.service.blogservice;
import com.lyy.blog.service.tagsservice;
import com.lyy.blog.service.typeservice;
import com.lyy.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class blogcontroller {


    @Autowired
    private blogservice blogservice;
    @Autowired
    private  typeservice typeservice;
    @Autowired
    private tagsservice tagsservice;
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size=2,sort = {"updateTime"},
            direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model){

        model.addAttribute("page",blogservice.listblog(pageable,blog));
        model.addAttribute("types",typeservice.listtype());

        return "admin/blogs";
    }


    /*定义一个方法只返回一个局部页面，就是blog页面的数据*/
    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 2, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blog, Model model) {
        model.addAttribute("page", blogservice.listblog(pageable, blog));
        return "admin/blogs :: blogList";
    }


    /*返回写博客页面*/
    @GetMapping("/blogs/input")
    public String input(Model model){

        model.addAttribute("blog",new blog());

        //初始化标签和分类
        setTypeAndTag(model);
        return "admin/blogs-input";
    }


    private void setTypeAndTag(Model model) {
        model.addAttribute("types", typeservice.listtype());
        model.addAttribute("tags", tagsservice.listTag());
    }

    @GetMapping("/blogs/{id}/input")
    public String editinput(@PathVariable Long id, Model model){

        //初始化标签和分类
        setTypeAndTag(model);
        blog blog=blogservice.getblog(id);
        blog.init();

        model.addAttribute("blog",blog);



        return "admin/blogs-input";
    }



    @PostMapping("/blogs")
    public String post(blog blog, RedirectAttributes attributes, HttpSession session){

        blog .setUser((user) session.getAttribute("user"));
        blog.setType(typeservice.getType(blog.getType().getId()));
        blog.setTags(tagsservice.listTag(blog.getTagIds()));
        blog b;
        if (blog.getId() == null) {
            b =  blogservice.savablog(blog);
        } else {
            b = blogservice.updateblog(blog.getId(), blog);
        }

        if(b==null){

            attributes.addFlashAttribute("message","操作失败");
        }else{

            attributes.addFlashAttribute("message","操作成功");
        }


        return "redirect:/admin/blogs";
    }


    @GetMapping("blogs/{id}/delete")
    public String deleteboke(@PathVariable Long id, RedirectAttributes attributes){
        blogservice.deleteblog(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }

}
